package app.baseinfomanage.organmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataIntegrityViolationException;

import app.base.common.Utility;

import app.baseinfomanage.organmanage.dao.OrganInfoDAO;
import app.baseinfomanage.organmanage.model.OrganInfo;
import app.baseinfomanage.organmanage.model.OrganInfoKey;

public class OrganHandle extends HttpServlet{

	private static final long serialVersionUID = 1L;
		
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
						
		String cmd = request.getParameter("SYS_OP_CMD");
		
		String result = null;

		if (cmd == null || cmd.trim().length() == 0)
		{
			result = "{returnCode: '-1', msg: '客户端的数据请求命令参数CMD为设置错误'}";
		}
		else
		{
			cmd = cmd.trim();
			
			if (cmd.equals(Utility.SYS_OPERATION_COMMAND_ADD) || cmd.equals(Utility.SYS_OPERATION_COMMAND_MODIFY))
			{
				OrganInfo record = new OrganInfo();
				
				if (cmd.equals(Utility.SYS_OPERATION_COMMAND_MODIFY))
				{
					record.setORGAN_ID(URLDecoder.decode(request.getParameter("ORGAN_ID").trim(), Utility.CHARSET_ENCODING_UTF8));
				}
	
				record.setORGAN_PARENT_ID(URLDecoder.decode(request.getParameter("ORGAN_PARENT_ID").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setORGAN_NAME(URLDecoder.decode(request.getParameter("ORGAN_NAME").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setORGAN_DESC(URLDecoder.decode(request.getParameter("ORGAN_DESC").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setNORMAL_IMG_URL(URLDecoder.decode(request.getParameter("NORMAL_IMG_URL").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setEXPAND_IMG_URL(URLDecoder.decode(request.getParameter("EXPAND_IMG_URL").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setLEAF_NODE_FLAG(URLDecoder.decode(request.getParameter("LEAF_NODE_FLAG").trim(), Utility.CHARSET_ENCODING_UTF8));
				record.setDISPLAY_RANKING_INDEX(BigDecimal.valueOf(Long.valueOf(URLDecoder.decode(request.getParameter("DISPLAY_RANKING_INDEX").trim(), Utility.CHARSET_ENCODING_UTF8))));
	
				result = doSystemOperations(cmd, record);
			}
			else if (cmd.equals(Utility.SYS_OPERATION_COMMAND_DELETE))
			{
				result = doSystemOperations(cmd, URLDecoder.decode(request.getParameter("ORGAN_ID").trim(), Utility.CHARSET_ENCODING_UTF8));
			}
			else
			{
				result = "{returnCode: '-1', msg: '服务器不支持的数据请求命令，请检查后再重试'}";
			}			
			
		}
		
		out.println(result);
		
		out.flush();
		out.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	private String doSystemOperations(String action, Object o)
	{
		
		String result = "{returnCode: '-1', msg: '系统调用函数 doSystemOperations() 失败'}";
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		OrganInfoDAO organInfoDAO = (OrganInfoDAO)context.getBean(Utility.ORGAN_MANAGE_MODULE_BEAN_NAME);
		
		if (action.equals(Utility.SYS_OPERATION_COMMAND_ADD))
		{
			result = insert(organInfoDAO, (OrganInfo) o);
		}
		else if (action.equals(Utility.SYS_OPERATION_COMMAND_MODIFY))
		{
			result = update(organInfoDAO, (OrganInfo) o);
		}
		else if (action.equals(Utility.SYS_OPERATION_COMMAND_DELETE))
		{			
			result = delete(organInfoDAO, (String) o);
		}
		
		return result;
	}
	
	private String insert(OrganInfoDAO organInfoDAO, OrganInfo record)
	{
		String result = "{returnCode: '0', msg: '新增组织机构成功'}";
		
		try {
			organInfoDAO.insert_0001(record);		
		} 
		catch(DataIntegrityViolationException e)
		{
			result = "{returnCode: '-1', msg: '系统不允许创建重复的组织机构'}";
		}
		catch (Exception e) {
			e.printStackTrace();
			result = "{returnCode: '-1', msg: " + e.toString() + "'}";
		}
		
		return result;
	}

	private String update(OrganInfoDAO organInfoDAO, OrganInfo record)
	{
		String result = "{returnCode: '0', msg: '修改系统组织机构成功'}";
		
		try {
			organInfoDAO.update_0001(record);		
		} 		
		catch(DataIntegrityViolationException e)
		{
			result = "{returnCode: '-1', msg: '系统不允许创建相同名称的多个组织机构'}";
		}
		catch (Exception e) {
			e.printStackTrace();
			result = "{returnCode: '-1', msg: " + e.toString() + "'}";
		}
		
		return result;
	}
	
	private String delete(OrganInfoDAO organInfoDAO, String organId)
	{
		String result = "{returnCode: '0', msg: '删除组织机构成功'}";
		
		try {
			OrganInfoKey organInfoKey = new OrganInfoKey();
			organInfoKey.setORGAN_ID(organId);
			organInfoDAO.delete_0001(organInfoKey);		
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			result = "{returnCode: '-1', msg: " + e.toString() + "'}";
		}
		
		return result;
	}	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}